package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.domain.ChatRecord;
import com.ruoyi.system.service.IChatRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 记录Controller
 *
 * @author trip
 * @date 2024-09-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/chatrecord")
public class ChatRecordController extends BaseController {

    private final IChatRecordService chatRecordService;

/**
 * 查询记录列表
 */
@PreAuthorize("@ss.hasPermi('system:chatrecord:list')")
@GetMapping("/list")
    public TableDataInfo list(ChatRecord chatRecord) {
        startPage();
        LambdaQueryWrapper<ChatRecord> lqw = new LambdaQueryWrapper<ChatRecord>();
                        if (chatRecord.getUserId() != null){
                        lqw.eq(ChatRecord::getUserId ,chatRecord.getUserId());
                    }
                        if (StringUtils.isNotBlank(chatRecord.getDriverId())){
                        lqw.eq(ChatRecord::getDriverId ,chatRecord.getDriverId());
                    }
                        if (chatRecord.getStaffserviceId() != null){
                        lqw.eq(ChatRecord::getStaffserviceId ,chatRecord.getStaffserviceId());
                    }
                        if (StringUtils.isNotBlank(chatRecord.getRecordInfo())){
                        lqw.eq(ChatRecord::getRecordInfo ,chatRecord.getRecordInfo());
                    }
                        if (StringUtils.isNotBlank(chatRecord.getServiceIsWatch())){
                        lqw.eq(ChatRecord::getServiceIsWatch ,chatRecord.getServiceIsWatch());
                    }
                        if (StringUtils.isNotBlank(chatRecord.getClientIsWatch())){
                        lqw.eq(ChatRecord::getClientIsWatch ,chatRecord.getClientIsWatch());
                    }
        List<ChatRecord> list = chatRecordService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ChatRecord> util = new ExcelUtil<ChatRecord>(ChatRecord.class);
        util.importTemplateExcel(response, "记录数据");
    }

    /**
     * 导入记录数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ChatRecord> util = new ExcelUtil<ChatRecord>(ChatRecord.class);
        List<ChatRecord> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            chatRecordService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:export')")
    @Log(title = "记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatRecord chatRecord) {
        List<ChatRecord> list = chatRecordService.selectChatRecordList(chatRecord);
        ExcelUtil<ChatRecord> util = new ExcelUtil<ChatRecord>(ChatRecord.class);
        util.exportExcel(response, list, "记录数据");
    }

    /**
     * 获取记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return AjaxResult.success(chatRecordService.selectChatRecordByRecordId(recordId));
    }

    /**
     * 新增记录
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:add')")
    @Log(title = "记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatRecord chatRecord) {
        return toAjax(chatRecordService.insertChatRecord(chatRecord));
    }

    /**
     * 修改记录
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:edit')")
    @Log(title = "记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatRecord chatRecord) {
        return toAjax(chatRecordService.updateChatRecord(chatRecord));
    }

    /**
     * 删除记录
     */
    @PreAuthorize("@ss.hasPermi('system:chatrecord:remove')")
    @Log(title = "记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(chatRecordService.deleteChatRecordByRecordIds(recordIds));
    }
}


